package com.xwj.coupon.user.controller;

import com.xwj.coupon.user.dto.req.CouponCreatePaymentReqDTO;
import com.xwj.coupon.user.dto.req.CouponProcessPaymentReqDTO;
import com.xwj.coupon.user.dto.req.CouponProcessRefundReqDTO;
import com.xwj.coupon.user.framework.result.Result;
import com.xwj.coupon.user.framework.web.Results;
import com.xwj.coupon.user.service.CouponSettlementService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: XuWenjie
 * @Date: 2025-08-21
 * @Description: CouponSettlementController
 */
@RestController
@RequiredArgsConstructor
public class CouponSettlementController {

    private final CouponSettlementService couponSettlementService;

    @Operation(summary = "计算用户优惠券优惠金额", description = "")
    @GetMapping("/api/engine/user-coupon/compute-amount")
    public Result<Map<Long,Long>> getCouponAmount(@RequestParam List<Long> couponIds, @RequestParam Long goodAmount) {

        return Results.success(couponSettlementService.computeCouponAmount(couponIds, goodAmount));
    }

    @Operation(summary = "创建用户优惠券结算单", description = "用户下单时锁定使用的优惠券，一般由订单系统发起调用")
    @PostMapping("/api/engine/user-coupon/create-payment-record")
    public Result<Void> createPaymentRecord(@RequestBody CouponCreatePaymentReqDTO requestParam) {
        couponSettlementService.createPaymentRecord(requestParam);
        return Results.success();
    }

    @Operation(summary = "核销优惠券结算单", description = "用户支付后核销使用的优惠券，常规来说应该监听支付后的消息队列事件")
    @PostMapping("/api/engine/user-coupon/process-payment")
    public Result<Void> processPayment(@RequestBody CouponProcessPaymentReqDTO requestParam) {
        couponSettlementService.processPayment(requestParam);
        return Results.success();
    }

    @Operation(summary = "退款优惠券结算单", description = "用户退款成功后返回使用的优惠券，常规来说应该监听退款成功后的消息队列事件")
    @PostMapping("/api/engine/user-coupon/process-refund")
    public Result<Void> processRefund(@RequestBody CouponProcessRefundReqDTO requestParam) {
        couponSettlementService.processRefund(requestParam);
        return Results.success();
    }
}
